﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
/*处理斜体？？？？
 <div id="Content">
          
          <figure class="image" style="display: table;"> 
 <img src="//img2.chinadaily.com.cn/images/202011/02/5f9f6b05a31024adbd9d91d8.jpeg" data-from="newsroom" data-mimetype="image/jpeg" id="img-5f9f6b05a31024adbd9d91d8"> 
 <figcaption style="display: table-caption; caption-side: bottom;">
   Black glazed censer, Deqing ware, Eastern Jin Dynasty (317-420), the Zhejiang Provincial Museum collection [Photo/zjmuex.com] 
 </figcaption> 
</figure> 文章的内容在content里面
 */
namespace Test_01
{
    class Crawler
    {
        public string SolveTag(string s)
        {
            int start = 0;
            int last = 0;
            for(int i=0;i<s.Length;i++)
            {
                if(s[i]=='<')
                {
                    start = i;
                }
                else if(s[i]=='>')
                {
                    last = i;
                    s=s.Remove(start, last - start + 1);///adsasd<we>qwe
                    i -= (last - start + 1);
                }
            }
            return s;
        }
        public  List<string> GetAddress(string ad1, string ad2)//是否要返回？？//<div class="mb10 tw3_01_2 ">从www.chinadaily.com.cn/life/fashion源码中找到其中的文章网址
        {
            List<string> res = new List<string>();
            string url = "http://www.chinadaily.com.cn/" + ad1 + "/" + ad2;
            WebClient wc = new WebClient();
            Regex reg = new Regex("<div class=\"mb10 tw3_01_2 \">[\\s\\S]*?href=\"//(.*)\">");
            byte[] htmlData = wc.DownloadData(url);
            string html = Encoding.UTF8.GetString(htmlData);
            MatchCollection result = reg.Matches(html);
            foreach (Match item in result)
            {
                res.Add("http://" + item.Groups[1]);
            }//这里返回的url就是存放文章的url 顺序与上面的一致
            return res;
        }
        public  List<string> GetPic(string ad1, string ad2)//是否要返回？？//<div class="mb10 tw3_01_2 ">从www.chinadaily.com.cn/life/fashion源码中找到其中的文章网址
        {//返回图片的地址
            List<string> res = new List<string>();
            string url = "http://www.chinadaily.com.cn/" + ad1 + "/" + ad2;
            WebClient wc = new WebClient();
            Regex reg = new Regex("<div class=\"mb10 tw3_01_2 \">[\\s\\S]*?src=\"//(.*)\"");
            byte[] htmlData = wc.DownloadData(url);
            string html = Encoding.UTF8.GetString(htmlData);
            MatchCollection result = reg.Matches(html);
            foreach (Match item in result)
            {
                res.Add("http://" + item.Groups[1]);
            }//这里打印的url就是存放图片的url
            return res;
        }

        public List<string> GetTitle(string ad1, string ad2)
        {
            List<string> res = new List<string>();
            string url = "http://www.chinadaily.com.cn/" + ad1 + "/" + ad2;
            WebClient wc = new WebClient();
            Regex reg = new Regex("<div class=\"mb10 tw3_01_2 \">[\\s\\S]*?<h4>[\\s\\S]*?>([\\s\\S]*?)</a>");
            byte[] htmlData = wc.DownloadData(url);
            string html = Encoding.UTF8.GetString(htmlData);
            MatchCollection result = reg.Matches(html);
            foreach (Match item in result)
            {
                res.Add("" + item.Groups[1]);     
            }

            return res;
        }
        public  string GetArticle(String address)//的输入是getarticle的输出
        {
            string s = "";
            WebClient wc = new WebClient();
            //地址由调用时传入
            byte[] htmlData = wc.DownloadData(address);

            string html = Encoding.UTF8.GetString(htmlData);

            //使用正则表达式组匹配   
            Regex reg = new Regex("<p>(.*)</p>");

            //接受所有匹配到的项
            MatchCollection result = reg.Matches(html);
            //循环输出
            foreach (Match item in result)
            {
                //0的话是整体匹配到的字符串对象
                //1就是第一个匹配到的组(\\S{100,)
                s += "  ";
                s += item.Groups[1];
                s += "\n";
                //Console.WriteLine(item.Groups[1]);
            }
            return s;
        }


        /*public static void CreatFile()
        {
            if (Directory.Exists(@"M:\Picture\"))
            {
                Console.WriteLine("\n————————开始——————————");
            }
            else
            {
                DirectoryInfo directory = new DirectoryInfo(@"M:\Picture\");
                directory.Create();
            }
        }*/

    }


}

